<?

class Registration extends Controller {

	function Registration()
	{
		parent::Controller();	
	}

   
    function index()
    {
    if($_POST['action']=="new"){
    $redirect="/store/";
    }else{
    $redirect=$_SERVER['HTTP_REFERER'];
    }
    
		//Load
		$this->load->helper('url');
		$this->load->library('validation');
		
		//Check incoming variables
		$rules['login']	= "required|max_length[32]|trim";
		$rules['pass']	= "required|min_length[4]|max_length[32]|trim";
    $rules['name']	= "required|trim";		

		$this->validation->set_rules($rules);

				
		if ($this->validation->run() == false) {
		      header( "Location: $redirect") ;				
		  }else{
		  
		      $old_session=$this->session->userdata('session_id');
		  
			//Create account
			if($this->simplelogin->create(
       $this->input->post('login'),
       $this->input->post('pass'), 
       $this->input->post('name'),
       $this->input->post('phone'),
       $this->input->post('city'),
       $this->input->post('index'),
       $this->input->post('adres')
       )) {
        	$query = $this->db->query("SELECT id FROM session_to_store WHERE session='$old_session'");
          								
          	if ($query->num_rows() > 0){

            $data['session']=$this->session->userdata('session_id');
            $this->db->update('session_to_store', $data, array('session' => $old_session));
            }

        	header( "Location: $redirect") ;		

        }else{
		       header( "Location: $redirect") ;
			}			
		}

   }
   
   	function logout() {
   	$redirect=$_SERVER['HTTP_REFERER'];
   	$this->load->helper('url');
		//Put here for PHP 4 users
		$this->CI =& get_instance();		

    $old_session=$this->session->userdata('session_id');
		//Destroy session
    $this->CI->session->sess_destroy();
    
    //New session
    $this->CI->session->sess_create();
    
    		          $query = $this->db->query("SELECT id FROM session_to_store WHERE session='$old_session'");
          								
          				if ($query->num_rows() > 0){

                			$data['session']=$this->session->userdata('session_id');
                      $this->db->update('session_to_store', $data, array('session' => $old_session));

                  }
		
    
    header( "Location: $redirect") ;		
	}
   
   
	function login()
	{
	 if($this->input->post('from') == 'buy'){
    $redirect='/store/buy/pass';
   }else{
   $redirect=$_SERVER['HTTP_REFERER'];
   }
   
		//Load
		$this->load->helper('url');
		$this->load->library('validation');
		
		//Check incoming variables
		$rules['login']	= "required|trim";
		$rules['pass']	= "required|trim|min_length[4]";		

		$this->validation->set_rules($rules);

		$fields['login_'] = 'Username';
		$fields['pass'] = 'Password';
		
		$this->validation->set_fields($fields);
				
		if ($this->validation->run() == false) {

			header( "Location: $redirect") ;		
		} else {
		
		  $old_session=$this->session->userdata('session_id');
			
      //login
			if($this->simplelogin->login($this->input->post('login'), $this->input->post('pass'))) {
		
		              $query = $this->db->query("SELECT id FROM session_to_store WHERE session='$old_session'");
          								
          				if ($query->num_rows() > 0){

                			$data['session']=$this->session->userdata('session_id');
                      $this->db->update('session_to_store', $data, array('session' => $old_session));

                  }
			
				header( "Location: $redirect") ;	
			} else {
				header( "Location: $redirect") ;			
			}			
		}
	}
   
   
   function edit()
   {
          
        if(!$this->session->userdata('logged_in')) {
            header( "Location: /store") ;
        }else{
        
           $username=$this->session->userdata('id');
           $query = $this->db->query("SELECT * FROM users WHERE id='$username' LIMIT 1");
          
        
        $data['row']=$query->row();
        
        
        
        $this->load->view('global/top');
    		$this->load->view('store/menu');
    		
    		$this->load->view('registration/edit', $data);
    		
   	 		$this->load->view('global/bottom');
     
        }
      

  
   
   }
   
   
   
      function save()
    {
        $this->load->helper('url');
		    $this->load->library('validation');
				
        if(!$this->session->userdata('logged_in')) {
            header( "Location: /store") ;
        }else{
    
           $username=$this->session->userdata('id');
           $query = $this->db->query("SELECT name, username FROM users WHERE id='$username' LIMIT 1");
        
              if($query->num_rows() == 1){
                
                	$rules['login']	= "required|trim";
		            $rules['name']	= "required|trim";
                    $rules['phone']	= "trim";
                    $rules['city']	= "trim";
                    $rules['index']	= "trim";
                    $rules['adres']	= "trim";				

		                $this->validation->set_rules($rules);
		                
		                		if ($this->validation->run() == false) {
                         redirect("/registration/edit");
		                		}else{
		                		    
		                		   $login=$this->validation->login;
		                	     $query = $this->db->query("SELECT username FROM users WHERE username='$login' AND id != '$_POST[id]'");
                            
                            
                          if($query->num_rows() == 0){

                          //Заносим изменения БД
                            
                  	   		  	$data['name']= $this->validation->name;
                  	   		  	$data['username']= $this->validation->login;
                  	   		  	$data['phone']= $this->validation->phone;
                  	   		  	$data['city']= $this->validation->city;
                  	   		  	$data['index']= $this->validation->index;
                  	   		  	$data['adres']= $this->validation->adres;
                  	     		
  	     	
                       				$this->db->where('id', $_POST['id']);
                              $this->db->update('users', $data);
                 
                              
                              redirect("/registration/edit");
                          
                          }else{
                             redirect("/registration/edit");
                         }

                  }

     
        }else{
             
             redirect("/registration/edit");
        }
      

  
   
   }
   
	
	}
	
	function lost($valid)
	{
  $this->load->helper('email');
  $this->load->library('email');
  $this->load->helper('url');
  $this->load->helper('security');
 
    $this->load->view('global/top');
    $this->load->view('store/menu');
  
    if(!isset($_POST['mail'])){
        
        if($valid){
        $data['valid']=$valid;
        }
 
        $this->load->view('registration/lost', $data);
    
    
    }else{
    
    $mail=trim($_POST['mail']);
    
    if (!valid_email("$mail")){
      redirect ('/registration/lost/true');
    }
    
            $this->email->from('store@rybalka.com.ua', 'Rybalka.com.ua');
            $this->email->to($mail);
            $this->email->subject('Новый пароль');
            
            
            
                  $if_exits = $this->db->query("SELECT id FROM users WHERE username='$mail' LIMIT 1");
              		if ($if_exits->num_rows() > 0){
              		

              		    include ('./classes/generate_pass.php');
              		    
              		    $new_pass=generatePassword();
              		    
              		    $md5 = dohash($new_pass, 'md5'); // MD5
              		          
              		    $data['password'] = $md5;
                      $this->db->where('username', $mail);
                      
                      if($this->db->update('users', $data)){
                        $this->email->message("<p>Ваш новый пароль для доступа к магазину: $new_pass</p><p><small><font color=\"gray\"><a href=\"http://www.rybalka.com.ua\">Rybalka.com.ua</a> — интернет-магазин рыболовный снастей</font></small></p>");
                        $this->email->send();
                      }
                      
              		        
                          
              		
              		
              		}else{
                    redirect ('/registration/lost/no_user');
                  }
            
            

            
          
    
    
    $data['mail']=$mail;
    $this->load->view('registration/send_lost', $data);
    
    }

  
    $this->load->view('global/bottom');

  }
  
  function new_user(){
  $this->load->view('global/top');
  $this->load->view('store/menu');
  
  $this->load->view('registration/registration');
  
  $this->load->view('global/bottom');
  
  }
	
}
	
?>